import { FakeApi, delay } from '@wowon/utils';
import { getCurrentUser } from '../../auth-center';
import { menuList } from './menu';

// 角色绑定的菜单数据
const role_menus = {
  'id-developer': [
    'r0',
    'r0m0',
    'r0m1',
    'r0m100',
    'r0m101',
    'r0m102',
    'r0m103',
    'r0m104',
    'r0m2',
    'r0m200',
    'r0m201',
    'r0m20101',
    'r0m20102',
    'r0m20103',
    'r0m202',
    'r0m20201',
    'r0m20202',
    'r0m20203',
    'r0m203',
    'r0m3',
    'r0m300',
    'r0m301',
    'r0m302',
    'r0m30201',
    'r0m303',
    'r0m304',
    'r0m305',
    'r0m306',
    'r0m4',
    'r0m400',
    'r0m401',
    'r0m402',
    'r0m403',
    'r0m5',
    'r0m500',
    'r0m501',
    'r0m502',
    'r1',
    'r1m1',
    'r1m101',
    'r1m102',
    'r2',
    'r2m1',
  ],
  'id-admin': [
    'r0m0',
    'r0m1',
    'r0m5',
    'r0m100',
    'r0m101',
    'r0m102',
    'r0m103',
    'r0m104',
    'r0m200',
    'r0m201',
    'r0m202',
    'r0m300',
    'r0m301',
    'r0m302',
    'r0m30201',
    'r0m303',
    'r0m304',
    'r0m400',
    'r0m401',
    'r0m402',
    'r0m500',
    'r0m501',
    'r0m502',
    'r0m20101',
    'r0m20102',
    'r0m20103',
    'r0m20201',
    'r0m20202',
    'r0m20203',
  ],
  'id-user': [
    'r0m300',
    'r0m301',
    'r0m302',
    'r0m30201',
    'r0m303',
    'r0m304',
    'r0m5',
    'r0m500',
    'r0m501',
    'r0m502',
    'r1',
    'r1m1',
    'r1m101',
    'r1m102',
    'r2',
    'r2m1',
  ],
  'id-guest': [
    'r0m0',
    'r0m100',
    'r0m101',
    'r0m102',
    'r0m103',
    'r0m104',
    'r0m200',
    'r0m20101',
    'r0m20201',
    'r0m203',
    'r0m3',
    'r0m300',
    'r0m301',
    'r0m302',
    'r0m30201',
    'r0m303',
    'r0m304',
    'r0m305',
    'r0m306',
    'r0m400',
    'r0m401',
    'r0m402',
    'r0m5',
    'r0m500',
    'r0m501',
    'r0m502',
    'r1',
    'r1m1',
    'r1m101',
    'r1m102',
    'r2',
    'r2m1',
  ],
};

const initData = [
  { id: 'id-developer', name: '开发者', key: 'developer', enable: 1, remark: '开发者' },
  { id: 'id-admin', name: '管理员', key: 'admin', enable: 1, remark: '系统管理员' },
  { id: 'id-user', name: '普通用户', key: 'user', enable: 0, remark: '系统用户' },
  { id: 'id-guest', name: '访客', key: 'guest', enable: 1, remark: '访客' },
];

export const roleList = new FakeApi(initData, {
  queryType: {
    id: 'is',
    key: 'like',
    name: 'like',
    enable: 'is',
  },
  sort: () => 0,
});

export default {
  /** 查询 */
  'GET /mock/role/query': async (req, res) => {
    const currentUser = getCurrentUser(req);
    if (!currentUser) {
      res.statusCode = 401;
      res.json({ code: '-1', message: '权限不足' });
      return;
    }
    const result = await roleList.query(req.query);
    res.json(result);
  },
  /** 新增 */
  'POST /mock/role/create': async (req, res) => {
    const currentUser = getCurrentUser(req);
    if (!currentUser) {
      res.statusCode = 401;
      res.json({ code: '-1', message: '权限不足' });
      return;
    }
    const result = await roleList.create(req.body);
    res.json(result);
  },
  /** 更新 */
  'POST /mock/role/update': async (req, res) => {
    const currentUser = getCurrentUser(req);
    if (!currentUser) {
      res.statusCode = 401;
      res.json({ code: '-1', message: '权限不足' });
      return;
    }
    const result = await roleList.update(req.body);
    res.json(result);
  },
  /** 删除 */
  'POST /mock/role/remove': async (req, res) => {
    const currentUser = getCurrentUser(req);
    if (!currentUser) {
      res.statusCode = 401;
      res.json({ code: '-1', message: '权限不足' });
      return;
    }
    delete role_menus[req.body.id];
    const result = await roleList.remove(req.body);
    res.json(result);
  },
  /** 详情 */
  'GET /mock/role/profile': async (req, res) => {
    const currentUser = getCurrentUser(req);
    if (!currentUser) {
      res.statusCode = 401;
      res.json({ code: '-1', message: '权限不足' });
      return;
    }
    const result = await roleList.profile(req.query);
    res.json(result);
  },
  /** 启用/禁用 */
  'POST /mock/role/enable': async (req, res) => {
    const currentUser = getCurrentUser(req);
    if (!currentUser) {
      res.statusCode = 401;
      res.json({ code: '-1', message: '权限不足' });
      return;
    }
    const { id, enable } = req.body;
    const result = await roleList.update({ id, enable });
    res.json(result);
  },
  /** 列表 */
  'GET /mock/role/list': async (req, res) => {
    const currentUser = getCurrentUser(req);
    if (!currentUser) {
      res.statusCode = 401;
      res.json({ code: '-1', message: '权限不足' });
      return;
    }
    const result = await roleList.list({ enable: Number(req.query.enable) });
    res.json(result);
  },
  /** 添加菜单 */
  'POST /mock/role/menu/bind': async (req, res) => {
    const currentUser = getCurrentUser(req);
    if (!currentUser) {
      res.statusCode = 401;
      res.json({ code: '-1', message: '权限不足' });
      return;
    }
    const { role_id, menu_ids } = req.body;
    const role = roleList.getItem({ id: role_id });

    if (!role) {
      res.json({ code: '-1', message: '查询不到此角色！' });
    }

    role_menus[role_id] = menu_ids;

    await delay(200);
    res.json({ code: '0', message: '成功！' });
  },
  /** 查询菜单绑定列表 */
  'GET /mock/role/menu/selected': async (req, res) => {
    const currentUser = getCurrentUser(req);
    if (!currentUser) {
      res.statusCode = 401;
      res.json({ code: '-1', message: '权限不足' });
      return;
    }
    const { role_id } = req.query;

    const list = role_menus[role_id] || [];

    await delay(200);
    res.json({ code: '0', list, message: '成功！' });
  },
  /** 查询菜单 */
  'GET /mock/role/menu': async (req, res) => {
    const currentUser = getCurrentUser(req);
    if (!currentUser) {
      res.statusCode = 401;
      res.json({ code: '-1', message: '权限不足' });
      return;
    }
    const { role_id } = req.query;

    const list = (role_menus[role_id] || []).map((id) => menuList.getItem({ id })).filter((i) => !!i);

    await delay(200);
    res.json({ code: '0', list, menuList: menuList._list, message: '成功！' });
  },
};
